<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Model\User;
use App\Model\JiscarGift;
use App\Model\JiscarLotteryRecord;

class ExportDataService extends BaseService
{

    public function lotteryRecord()
    {
        $lotteryRecord = JiscarLotteryRecord::query()->with('gift')->whereBetween('created_at', ['2023-11-30','2023-12-01'])->get();
        foreach($lotteryRecord as $val)
        {
            if($val['gift_id'] == 0)
            {
                $giftName = '谢谢参与';
            }else{
                $giftName = $val['gift']['name'];
            }
            $exportData[] = [$val['ip'], $giftName, $val['cdk'], $val['created_at']];
        }

        $titleArr = ['IP', '礼物', 'CDK', '时间'];

        $relativePath = '/public/export_data/';       //相对路径
        $filePath = BASE_PATH . $relativePath;        //存储路径
        if(!is_dir($filePath))
        {
            mkdir($filePath, 0777, true);
        }
        $fileName = get_rand_str();     //重新生成文件名
        $fullPath = $filePath.$fileName.'.csv';     //完整文件路径

        export_to_csv($fullPath, $titleArr, $exportData);

        return [];
    }

    public function user()
    {
        $user = User::query()->select('id', 'account','level','balance','star_currency','points','nickname','fullname','birthday','gender','signature','wechat','qq','phone','steam')->where('status', 1)->where('deleted', 0)->get()->toArray();
        foreach($user as $val)
        {
            $exportData[] = $val;
        }


        $titleArr = ['ID', '账号', '等级', '余额', '星币', '积分', '昵称', '姓名', '生日', '性别', '签名', '微信', 'QQ', '手机号', 'steam'];

        $relativePath = '/public/export_data/';       //相对路径
        $filePath = BASE_PATH . $relativePath;        //存储路径
        if(!is_dir($filePath))
        {
            mkdir($filePath, 0777, true);
        }
        $fileName = get_rand_str();     //重新生成文件名
        $fullPath = $filePath.$fileName.'.csv';     //完整文件路径

        export_to_csv($fullPath, $titleArr, $exportData);

        return [];
    }

}